package cn.edu.scut.hsrc.util;

import java.util.Calendar;

/**
 * 时间工具
 * 
 * @file TimeSpanUtil.java
 * @author feiyu
 * 
 */
public class TimeSpanUtil {

	public enum TimeOption {
		ANY, // 时间不限
		LATEST, // 最新结果
		DAY, // 最近一天
		WEEK, // 最近一周
		MONTH, // 最近一个月
		YEAR; // 最近一年
	};

	/**
	 * 
	 * @param curr
	 * @param option
	 */

	public long getSpot(Calendar curr, TimeOption option) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTimeInMillis(curr.getTimeInMillis());
		long result = -1;
		switch (option) {
		case LATEST:
			result = calLatest(calendar);
			break;
		case DAY:
			result = calDay(calendar);
			break;
		case WEEK:
			result = calWeek(calendar);
			break;
		case MONTH:
			result = calMonth(calendar);
			break;
		case YEAR:
			result = calYear(calendar);
			break;
		case ANY:
			result = calAny(calendar);
			break;
		}
		return result;
	}

	/**
	 * 最近结果的时间（当前时间的前两个小时）
	 * 
	 * @param curr
	 * @return
	 */
	private long calLatest(Calendar curr) {
		curr.set(Calendar.HOUR_OF_DAY, curr.get(Calendar.HOUR_OF_DAY) - 2);
		return curr.getTimeInMillis();
	}

	/**
	 * 最近一天
	 * 
	 * @param curr
	 * @return
	 */
	private long calDay(Calendar curr) {
		curr.set(Calendar.DAY_OF_WEEK, curr.get(Calendar.DAY_OF_WEEK) - 1);
		return curr.getTimeInMillis();
	}

	/**
	 * 最近一周
	 * 
	 * @param curr
	 * @return
	 */
	private long calWeek(Calendar curr) {
		curr.set(Calendar.DAY_OF_MONTH, curr.get(Calendar.DAY_OF_MONTH) - 7);
		return curr.getTimeInMillis();
	}

	/**
	 * 最近一个月
	 * 
	 * @param curr
	 * @return
	 */
	private long calMonth(Calendar curr) {
		curr.set(Calendar.MONTH, curr.get(Calendar.MONTH) - 1);
		return curr.getTimeInMillis();
	}

	/**
	 * 最近一年
	 * 
	 * @param curr
	 * @return
	 */
	private long calYear(Calendar curr) {
		curr.set(Calendar.YEAR, curr.get(Calendar.YEAR) - 1);
		return curr.getTimeInMillis();
	}

	/**
	 * 时间不限
	 * 
	 * @param curr
	 */
	private long calAny(Calendar curr) {
		return 0;
	}

	/**
	 * 获取现在的时间
	 * 
	 * @return
	 */
	public static Calendar getCurrentTime() {
		return Calendar.getInstance();
	}

	public static void main(String[] args) {
		TimeSpanUtil timeSpanUtil = new TimeSpanUtil();

		// util.calculate(TimeOption.ANY);

		Calendar calendar = Calendar.getInstance();
		calendar.set(2010, 9, 1, 20, 8, 9); // 月份是基于0的，例如：9表示10月
		long currTime = calendar.getTimeInMillis();
		System.out.println(currTime);
		
		
		System.out.println(timeSpanUtil.getSpot(calendar,TimeOption.LATEST)-currTime);
		System.out.println(timeSpanUtil.getSpot(calendar,TimeOption.DAY)-currTime);
		System.out.println(timeSpanUtil.getSpot(calendar,TimeOption.WEEK)-currTime);
		System.out.println(timeSpanUtil.getSpot(calendar,TimeOption.MONTH)-currTime);
		System.out.println(timeSpanUtil.getSpot(calendar,TimeOption.YEAR)-currTime);
		System.out.println(timeSpanUtil.getSpot(calendar,TimeOption.ANY)-currTime);
	}
}
